<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('http://yoursite.com').hostname,
    root: '/',
    scheme: 'Pisces',
    version: '7.6.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="考虑确定平面上n个点(x1, y1),(x2,y2)…(xn,yn)集合上的最近点对的问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="最近点对问题">
<meta property="og:url" content="http://yoursite.com/2019/10/19/e6-9c-80-e8-bf-91-e7-82-b9-e5-af-b9-e9-97-ae-e9-a2-98/index.html">
<meta property="og:site_name" content="SRCNN">
<meta property="og:description" content="考虑确定平面上n个点(x1, y1),(x2,y2)…(xn,yn)集合上的最近点对的问题。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2019-10-19T05:39:06.000Z">
<meta property="article:modified_time" content="2020-02-18T02:48:47.115Z">
<meta property="article:author" content="Z.J. Jiang">
<meta property="article:tag" content="SISR">
<meta property="article:tag" content=" FH">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2019/10/19/e6-9c-80-e8-bf-91-e7-82-b9-e5-af-b9-e9-97-ae-e9-a2-98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>最近点对问题 | SRCNN</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">SRCNN</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">SISR-FH</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/19/e6-9c-80-e8-bf-91-e7-82-b9-e5-af-b9-e9-97-ae-e9-a2-98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Z.J. Jiang">
      <meta itemprop="description" content="about the single image super-resolution and face hallucination">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="SRCNN">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          最近点对问题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2019-10-19 13:39:06" itemprop="dateCreated datePublished" datetime="2019-10-19T13:39:06+08:00">2019-10-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-02-18 10:48:47" itemprop="dateModified" datetime="2020-02-18T10:48:47+08:00">2020-02-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/mathematics/" itemprop="url" rel="index">
                    <span itemprop="name">mathematics</span>
                  </a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>考虑确定平面上n个点(x1, y1),(x2,y2)…(xn,yn)集合上的最近点对的问题。<br><a id="more"></a></p>
<pre><code>可以很简单的写出暴力法破解：
// Points 为所有点对的集合，using Point = pair&lt;double, double&gt;, 
// a, b 传出参数， 最近点对的两个点。
double get_distance_enforce(const vector&lt;Point&gt; &amp;Points, Point &amp;a, Point &amp;b)
{
    double distance = static_cast&lt;double&gt;(INT32_MAX);
    for (auto i = Points.begin(); i != Points.end(); ++i)
    {
        for (auto j = i + 1; j != Points.end(); ++j)
        {
            double tmp = sqrt(distance_square(*i, *j));
            // 如果跨分割线的两点距离小于已知距离，则记录该距离。
            if (tmp &lt; distance)
            {
                distance = tmp;
                a = *i;
                b = *j;
            }
        }
    }
    return distance;
}
这样得出的算法复杂度为O(n^2).
</code></pre><p>方法2：可以用递归的方法，分而治之，将一个大问题分解为若干个小问题。</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;random&gt;
#include &lt;chrono&gt;
#include &lt;cmath&gt;
#include &lt;algorithm&gt;
using namespace std;
using Point = pair&lt;double, double&gt;;
constexpr int NO_DISTANCE = 10000;

// 随机生成点对
void set_point(vector&lt;Point&gt; &amp;Points, int size, double x, double y)
{
    std::default_random_engine e(std::chrono::system_clock().now().time_since_epoch().count());
    std::uniform_real_distribution&lt;double&gt; dis(x, y);
    for (auto i = 0; i &lt; size; ++i)
    {
        Points.push_back(Point(dis(e), dis(e)));
    }
}

// 打印点对
void print_points(const vector&lt;Point&gt; &amp;Points)
{
    for (auto i : Points)
    {
        cout &lt;&lt; &quot;(&quot; &lt;&lt; i.first &lt;&lt; &quot;, &quot; &lt;&lt; i.second &lt;&lt; &quot;)&quot; &lt;&lt; endl;
    }
    cout &lt;&lt; endl;
}

double distance_square(Point a, Point b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}

// points 所有的点的集合， a, b 传出参数
// Points_X 按X排序的序列， Points_Y 按Y排序的序列。
double closestPoint(const vector&lt;Point&gt; &amp;Points_X, const vector&lt;Point&gt; &amp;Points_Y, Point &amp;a, Point &amp;b)
{
    Point a1, b1, a2, b2;
    int length = Points_X.size();
    double distance;
    if (length &lt; 2)
        return NO_DISTANCE;
    else if (length == 2)
    {
        a = Points_X[0];
        b = Points_X[1];
        distance = sqrt(distance_square(Points_X[0], Points_X[1]));
    }
    else
    {
        vector&lt;Point&gt; pst1(Points_X.begin(), Points_X.begin() + length / 2);
        vector&lt;Point&gt; pst2(Points_X.begin() + length / 2, Points_X.end());
        double d1 = closestPoint(pst1, Points_Y, a1, b1);
        double d2 = closestPoint(pst2, Points_Y, a2, b2);
        if (d1 &lt; d2)
        {
            distance = d1;
            a = a1;
            b = b1;
        }
        else
        {
            distance = d2;
            a = a2;
            b = b2;
        }

        // 一个点在左边 一个点在右边的情况。
        vector&lt;Point&gt; pst3;
        double mid = (Points_X.begin() + length / 2 - 1)-&gt;first;
        for (auto point : Points_Y)
        {
            if (abs(point.first - mid) &lt;= distance)
                pst3.push_back(point);
        }

        // 只需要与有序的链接的7个点进行比较就可以了。
        for (auto i = pst3.begin(); i != pst3.end(); ++i)
        {
            for (auto j = i + 1; j != i + 7 &amp;&amp; j != pst3.end(); ++j)
            {
                double tmp = sqrt(distance_square(*i, *j));
                // 如果跨分割线的两点距离小于已知距离，则记录该距离。
                if (tmp &lt; distance)
                {
                    distance = tmp;
                    a = *i;
                    b = *j;
                }
            }
        }
    }
    return distance;
}

double get_distance_enforce(const vector&lt;Point&gt; &amp;Points, Point &amp;a, Point &amp;b)
{
    double distance = static_cast&lt;double&gt;(INT32_MAX);
    for (auto i = Points.begin(); i != Points.end(); ++i)
    {
        for (auto j = i + 1; j != Points.end(); ++j)
        {
            double tmp = sqrt(distance_square(*i, *j));
            // 如果跨分割线的两点距离小于已知距离，则记录该距离。
            if (tmp &lt; distance)
            {
                distance = tmp;
                a = *i;
                b = *j;
            }
        }
    }
    return distance;
}
int main()
{
    int points_size;
    cout &lt;&lt; &quot;请输入要生成的points的数量 &quot;;
    cin &gt;&gt; points_size;

    cout &lt;&lt; &quot;********************************************************************************&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;随机生成的点是&quot; &lt;&lt; endl;
    vector&lt;Point&gt; Points;
    set_point(Points, points_size, -100.0, 100.0);
    print_points(Points);

    auto start1 = std::chrono::system_clock::now();
    vector&lt;Point&gt; Points_X(Points);
    vector&lt;Point&gt; Points_Y(Points);

    sort(Points_X.begin(), Points_X.end(), [](Point a, Point b) { return a.first &lt; b.first; });
    sort(Points_Y.begin(), Points_Y.end(), [](Point a, Point b) { return a.second &lt; b.second; });

    // cout &lt;&lt; &quot;********************************************************************************&quot; &lt;&lt; endl;
    // cout &lt;&lt; &quot;按照x升序排列&quot; &lt;&lt; endl;
    // print_points(Points_X);

    // cout &lt;&lt; &quot;********************************************************************************&quot; &lt;&lt; endl;
    // cout &lt;&lt; &quot;按照y升序排列&quot; &lt;&lt; endl;
    // print_points(Points_Y);

    Point a, b;
    cout &lt;&lt; &quot;********************************************************************************&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;归并法破解&quot; &lt;&lt; endl;
    double distance1 = closestPoint(Points_X, Points_Y, a, b);
    auto end1 = std::chrono::system_clock::now();
    auto duration1 = chrono::duration_cast&lt;chrono::microseconds&gt;(end1 - start1);
    cout &lt;&lt; &quot;最近的距离是&quot; &lt;&lt; distance1 &lt;&lt; endl;
    cout &lt;&lt; &quot;两个点分别是 (&quot; &lt;&lt; a.first &lt;&lt; &quot;, &quot; &lt;&lt; a.second &lt;&lt; &quot;), (&quot; &lt;&lt; b.first &lt;&lt; &quot;, &quot; &lt;&lt; b.second &lt;&lt; &quot;)&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;花费了&quot;
         &lt;&lt; double(duration1.count()) * chrono::microseconds::period::num / chrono::microseconds::period::den
         &lt;&lt; &quot;秒&quot; &lt;&lt; endl;

    Point c, d;
    cout &lt;&lt; &quot;********************************************************************************&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;暴力法破解&quot; &lt;&lt; endl;
    auto start2 = std::chrono::system_clock::now();
    double distance2 = get_distance_enforce(Points, c, d);
    auto end2 = std::chrono::system_clock::now();
    auto duration2 = chrono::duration_cast&lt;chrono::microseconds&gt;(end2 - start2);
    cout &lt;&lt; &quot;最近的距离是&quot; &lt;&lt; distance2 &lt;&lt; endl;
    cout &lt;&lt; &quot;两个点分别是 (&quot; &lt;&lt; c.first &lt;&lt; &quot;, &quot; &lt;&lt; c.second &lt;&lt; &quot;), (&quot; &lt;&lt; d.first &lt;&lt; &quot;, &quot; &lt;&lt; d.second &lt;&lt; &quot;)&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;花费了&quot;
         &lt;&lt; double(duration2.count()) * chrono::microseconds::period::num / chrono::microseconds::period::den
         &lt;&lt; &quot;秒&quot; &lt;&lt; endl;

    return 0;
}
</code></pre><p>方法2复杂度为O(nlogn)，还可以进一步简化，用左右界限来代替不必要的数组拷贝。</p>
<p>当n不够大时，其实暴力法需要的时间反而更少，当n特别大的时候，sorry，我并没有测试。</p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/10/16/e6-95-b0-e5-ad-a6-e5-bd-92-e7-ba-b3-e6-b3-95-e5-92-8c-e5-bc-ba-e5-bd-92-e7-ba-b3-e6-b3-95/" rel="prev" title="数学归纳法和强归纳法">
      <i class="fa fa-chevron-left"></i> 数学归纳法和强归纳法
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/12/24/SRCNN/" rel="next" title="SRCNN">
      SRCNN <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Z.J. Jiang</p>
  <div class="site-description" itemprop="description">about the single image super-resolution and face hallucination</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">42</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/jzijin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;jzijin" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://www.njust.edu.cn/" title="http:&#x2F;&#x2F;www.njust.edu.cn" rel="noopener" target="_blank">南京理工大学</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://ehall.njust.edu.cn/new/index.html" title="http:&#x2F;&#x2F;ehall.njust.edu.cn&#x2F;new&#x2F;index.html" rel="noopener" target="_blank">南京理工大学智慧服务</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://lib.njust.edu.cn/" title="http:&#x2F;&#x2F;lib.njust.edu.cn&#x2F;" rel="noopener" target="_blank">南京理工大学图书馆</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Z.J. Jiang</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.6.0
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
